export function shuffle (source) {
  const arr = source.slice()
  for (let i = 0; i < arr.length; i++) {
    const j = getRandomInt(i)
    swap(arr, i, j)
  }
  return arr
}

function getRandomInt (max) {
  return Math.floor(Math.random() * (max + 1))
}

function swap (arr, i, j) {
  const t = arr[i]
  arr[i] = arr[j]
  arr[j] = t
}

export function FormatTime (t) {
  t = t | 0
  const min = (((t / 60) | 0) + '').padStart(2, '0')
  const second = ((t % 60 | 0) + '').padStart(2, '0')
  return `${min}:${second}`
}

/**
[00:00.000] 作曲 : 许嵩
[00:01.000] 作词 : 许嵩
[00:22.240]天空好想下雨
[00:24.380]我好想住你隔壁
[00:26.810]傻站在你家楼下
[00:29.500]抬起头数乌云
[00:31.160]如果场景里出现一架钢琴
[00:33.640]我会唱歌给你听
[00:35.900]哪怕好多盆水往下淋
[00:41.060]夏天快要过去}
 */

// [00:31.160]如果场景里出现一架钢琴
const parseExp = /\[(\d{2}):(\d{2})\.(\d{2,3})\]/

export function parseLyric (lyricString) {
  const lineStrings = lyricString.split('\n')

  const lyrics = []
  for (const line of lineStrings) {
    if (line) {
      const result = parseExp.exec(line)
      if (!result) continue
      const time1 = result[1] * 60 * 1000
      const time2 = result[2] * 1000
      const time3 = result[3].length === 3 ? result[3] * 1 : result[3] * 10
      const time = time1 + time2 + time3
      const content = line.replace(parseExp, '').trim()
      const lineObj = { time, content }
      if (content && time) {
        lyrics.push(lineObj)
      }
    }
  }
  return lyrics
}
